import tensorflow as tf

w = tf.constant([2., 2.])
x = tf.constant([1., 1.])

with tf.GradientTape(persistent=True) as tape:
    tape.watch([w])
    yy = x * w
grad_1 = tape.gradient(yy, [w])
print(grad_1)
grad_1 = tape.gradient(yy, [w])
print(grad_1)

# 计算过程必须放置到tape内
y = x * w
with tf.GradientTape() as tape:
    tape.watch(w)
    yy = x * w
grad_1 = tape.gradient(y, [w])
print(grad_1)

print("=" * 200)
# 2nd derivation
w = tf.Variable(1.)
b = tf.Variable(2.)

x = tf.Variable(3.)

with tf.GradientTape() as tape_1:
    with tf.GradientTape() as tape_2:
        y = x * w + b
    dy_dw_1, dy_db_1 = tape_2.gradient(y, [w, b])
dy_dw_2 = tape_1.gradient(dy_dw_1, [w])

print(dy_dw_1)
print(dy_db_1)
print(dy_dw_2)

assert dy_dw_1.numpy() == 3.0
assert dy_dw_2[0] is None
